//
//  Controller13.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/7.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

// AnimatabletableView
class Controller13: UIViewController,UITableViewDataSource {

    var datas = ["Personal Life", "Buddy Company", "#30 days Swift Project", "Body movement training", "AppKitchen Studio", "Project Read", "Others" , "Fix the layout problem of a client project", "Write the experience of #30daysSwift", "Inbox Zero", "Booking the ticket to Chengdu", "Test the Adobe Project Comet", "Hop on a call to mom", "Fix the layout problem of a client project", "Write the experience of #30daysSwift", "Inbox Zero", "Booking the ticket to Chengdu", "Test the Adobe Project Comet", "Hop on a call to mom"]

    let tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI();
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func configUI(){
        let gradientLayer = CAGradientLayer();
        gradientLayer.frame         = self.view.bounds;
        gradientLayer.colors        = [UIColor.clear.cgColor,UIColor.black.cgColor];
        gradientLayer.locations     = [0.05,0.3];
        gradientLayer.startPoint    = CGPoint(x: 0.5, y: 0);
        gradientLayer.endPoint      = CGPoint(x: 0.5, y: 1);
        view.layer.mask = gradientLayer;
        
        view.addSubview(tableView);
        tableView.dataSource = self;
        tableView.tableFooterView = UIView();
        tableView.separatorStyle = .none;
        tableView.backgroundColor = .black;
        tableView.register(GradientCell.classForCoder(), forCellReuseIdentifier: "aaa")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "aaa", for: indexPath);
        cell.backgroundColor = colorforIndex(indexPath.row);
        cell.textLabel?.text = datas[indexPath.row];
        return cell;
    }
    
    func colorforIndex(_ index: Int) -> UIColor {
        let itemCount = datas.count - 1
        let color = (CGFloat(index) / CGFloat(itemCount)) * 0.6
        return UIColor(red: color, green: 0.0, blue: 1.0, alpha: 1.0)
    }

    
 
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        
        tableView.reloadData();
        let tableView_H = tableView.frame.height;
        
        let cells = tableView.visibleCells;
        for cell in cells {
            cell.transform = CGAffineTransform(translationX: 0, y: tableView_H);
        }
        
        var index = 0;
        
        for cell in cells {
            UIView.animate(withDuration: 1.0, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveLinear, animations: {
                cell.transform = CGAffineTransform(translationX: 0, y: 0);
            }, completion: nil)
            index += 1;
        }
        
    }
  
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
